package com.example.testapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.lang.ref.WeakReference;
import java.util.Random;

public class DiglettActivity extends ApplicationActivity implements View.OnClickListener, View.OnTouchListener {
    private TextView mResultTextView;
    private ImageView mDiglettImageView;
    private Button mStartButton;
    public static final int CODE = 123;

    // 坐标 二维数组 X Y 轴
    public int[][] mPosition = new int[][]{
            {342, 180},{432, 880},
            {521, 256},{429, 780},
            {456, 976},{145, 665},
            {123, 678},{564, 567},
    };
    // 点击次数
    private int mTotalCount;
    // 成功次数
    private int mSuccessCount;
    public static final int MAX_COUNT = 10;
    private DiglettHandler mHandler = new DiglettHandler(this);
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialet);

        mResultTextView = (TextView) findViewById(R.id.text_view);
        mDiglettImageView = (ImageView) findViewById(R.id.image_view);
        mStartButton = (Button) findViewById(R.id.start_button);

        mStartButton.setOnClickListener(this);
        mDiglettImageView.setOnTouchListener(this);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.start_button:
                start();
                break;
        }

    }

    private void start() {
        // 发送消息
        mResultTextView.setText("开始游戏");
        mStartButton.setText("游戏中");
        mStartButton.setEnabled(false);
        next(0);
    }
    // 下一步
    private void next(int delayTime){
        // 生成 0-7 范围的随机数
        int position = new Random().nextInt(mPosition.length);
        Message message = Message.obtain();
        message.what = CODE;
        message.arg1 = position;
        mHandler.sendMessageDelayed(message,delayTime);
        mTotalCount ++;


    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.setVisibility(View.GONE);
        mSuccessCount ++ ;
        mResultTextView.setText("打到了"+ mSuccessCount +"只,共" + MAX_COUNT +"只.");
        return false;
    }


    // 主线程
    public static class DiglettHandler extends Handler {
        public final WeakReference<DiglettActivity> mWeakReference;  //??
        public DiglettHandler(DiglettActivity activity) {
            mWeakReference = new WeakReference<>(activity);
        }
        @Override
        public void handleMessage(Message msg){
            super.handleMessage(msg);
            DiglettActivity activity = mWeakReference.get();

            switch (msg.what){
                case CODE:
                    if(activity.mTotalCount > MAX_COUNT){
                        activity.mTotalCount = 0;
                        activity.mSuccessCount = 0;
                        activity.mDiglettImageView.setVisibility(View.GONE);
                        activity.mStartButton.setText("点击开始");
                        activity.mStartButton.setEnabled(true);
                        Toast.makeText(activity, "地鼠打完了", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    int position = msg.arg1;
                    activity.mDiglettImageView.setX(activity.mPosition[position][0]);
                    activity.mDiglettImageView.setY(activity.mPosition[position][1]);
                    activity.mDiglettImageView.setVisibility(View.VISIBLE);
                    int randomTime = new Random().nextInt(500) + 500;
                    activity.next(randomTime);
                    break;
            }

        }
    }
    //  清除数据
    private void clear() {
        mTotalCount = 0;
        mSuccessCount = 0;
        mDiglettImageView.setVisibility(View.GONE);
        mStartButton.setText("点击开始");
        mStartButton.setEnabled(true);
    }
}
